﻿#region 命名空间

using System;
using System.IO;
using WinnerCMS.Common;
using WinnerCMS.PlugIn.OpenAPI.Config;

#endregion

namespace WinnerCMS.PlugIn.OpenAPI.Passport
{
    /// <summary>
    /// 远程应答接口,响应部分
    /// </summary>
    public class Service : System.Web.UI.Page
    {
        private readonly APIConfig _config;
        private MemberInfo _minfo = new MemberInfo();
        private readonly System.Web.HttpContext context;


        public Service(System.Web.HttpContext context)
        {
            this.context = context;
            this._config = APIConfigManage.GetConfig();
        }

        #region 检查验证票是否正确

        /// <summary>
        /// 检查验证票是否正确
        /// </summary>
        /// <param name="username">用户名</param>
        /// <param name="appKey">应用密钥</param>
        /// <param name="authToken">验证票</param>
        /// <returns>检查验证票是否正确</returns>
        private static bool validateAuth_token(string username, string appKey, string authToken)
        {
            if (Hash.Md5(string.Concat(username, appKey)).Equals(authToken, StringComparison.CurrentCultureIgnoreCase))
            {
                return true;
            }
            return false;
        }

        #endregion

        /// <summary>
        /// 返回信息到客端
        /// </summary>
        /// <param name="status"></param>
        /// <param name="strMsg"></param>
        private void ReturnResult(string status, string strMsg)
        {
            this._minfo.Status = status;
            this._minfo.Message = strMsg;
            this.context.Response.ContentType = "text/xml";
            this.context.Response.Charset = "utf-8";
            this.context.Response.Clear();

            this.context.Response.Write(PassportHelper.Serialize(this._minfo));
        }


        //开始处理请求
        public void DoWork()
        {
            //响应整合端请求
            if (this._config.DPOEnable == false)
            {
                ReturnResult("1", "系统并未开启整合接口！");
                return;
            }

            try
            {
                StreamReader reader1 = new StreamReader(this.context.Request.InputStream);

                string xml = reader1.ReadToEnd();
                this._minfo = SerializableHelper.DeSerialize<MemberInfo>(xml, DataFormart.Xml);
            }
            catch
            {
                ReturnResult("1", "解析数据出错!");
                return;
            }

            if (validateAuth_token(this._minfo.UserName, this._config.AppKey, this._minfo.Syskey) == false)
            {
                ReturnResult("1", string.Format("{0}提示您：{1}", this._minfo.Appid, "安全码校验失败!"));
                return;
            }

            switch (this._minfo.Action)
            {
                case "reguser": //注册新用户
                    Reguser();
                    break;
                case "login": //登录
                    Login();
                    break;
                case "PasswordModify": //修改密码
                    PasswordModify();
                    break;
                case "logout": //注销
                    Logout();
                    break;
                case "update": //更新用户信息
                    update();
                    break;
                case "delete": //删除用户
                    delete();
                    break;
            }
        }

        #region 注册新用户

        /// <summary>
        /// 注册新用户
        /// </summary>
        private void Reguser()
        {
            IMember i;
            try
            {
                i = PassportHelper.Instance(this._config.DLLName, this._config.FullCategoryName);
            }
            catch (Exception ex)
            {
                ReturnResult("1", string.Format("{0}提示您：{1}", this._minfo.Appid, ex.Message));
                return;
            }

            string Err = null;

            try
            {
                i.RegMember(this._minfo, ref Err);
            }
            catch (Exception ex)
            {
                ReturnResult("1", string.Format("{0}提示您：{1}", this._minfo.Appid, ex.Message));
                return;
            }
            if (string.IsNullOrEmpty(Err) == false)
            {
                ReturnResult("1", string.Format("{0}提示您：{1}", this._minfo.Appid, Err));
            }
        }

        #endregion

        #region 登录

        /// <summary>
        /// 登录
        /// </summary>
        private void Login()
        {
            IMember i;
            try
            {
                i = PassportHelper.Instance(this._config.DLLName, this._config.FullCategoryName);
            }
            catch (Exception ex)
            {
                ReturnResult("1", string.Format("{0}提示您：{1}", this._minfo.Appid, ex.Message));
                return;
            }

            string Err = null;
            try
            {
                i.TryLogin(this._minfo, ref Err);
            }
            catch (Exception ex)
            {
                ReturnResult("1", string.Format("{0}提示您：{1}", this._minfo.Appid, ex.Message));
                return;
            }
            if (string.IsNullOrEmpty(Err) == false)
            {
                ReturnResult("1", string.Format("{0}提示您：{1}", this._minfo.Appid, Err));
            }
        }

        #endregion

        private void PasswordModify()
        {
            IMember i;
            try
            {
                i = PassportHelper.Instance(this._config.DLLName, this._config.FullCategoryName);
            }
            catch (Exception ex)
            {
                ReturnResult("1", string.Format("{0}提示您：{1}", this._minfo.Appid, ex.Message));
                return;
            }
            string Err = null;
            i.PasswordModify(this._minfo.UserName, this._minfo.PassWord, ref Err);

            if (string.IsNullOrEmpty(Err) == false)
            {
                ReturnResult("1", string.Format("{0}提示您：{1}", this._minfo.Appid, Err));
            }
        }

        private void Logout()
        {
            IMember i;
            try
            {
                i = PassportHelper.Instance(this._config.DLLName, this._config.FullCategoryName);
            }
            catch (Exception ex)
            {
                ReturnResult("1", string.Format("{0}提示您：{1}", this._minfo.Appid, ex.Message));
                return;
            }
            i.MemberLogout(this._minfo.UserName);
        }

        private void update()
        {
            IMember i;
            try
            {
                i = PassportHelper.Instance(this._config.DLLName, this._config.FullCategoryName);
            }
            catch (Exception ex)
            {
                ReturnResult("1", string.Format("{0}提示您：{1}", this._minfo.Appid, ex.Message));
                return;
            }
            string Err = null;

            i.EditByMemberSelf(this._minfo, ref Err);
        }

        private void delete()
        {
            IMember i;
            try
            {
                i = PassportHelper.Instance(this._config.DLLName, this._config.FullCategoryName);
            }
            catch (Exception ex)
            {
                ReturnResult("1", string.Format("{0}提示您：{1}", this._minfo.Appid, ex.Message));
                return;
            }
            string Err = null;
            i.Delete(this._minfo.UserName, ref Err);
            if (string.IsNullOrEmpty(Err) == false)
            {
                ReturnResult("1", string.Format("{0}提示您：{1}", this._minfo.Appid, Err));
            }
        }
    }
}